package com.wtb.thread;

import java.util.concurrent.BlockingQueue;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.tencent.weibo.api.T_API;
import com.tencent.weibo.beans.OAuth;
import com.tencent.weibo.utils.WeiBoConst;
import com.wtb.domain.Config;
import com.wtb.domain.DataCache;
import com.wtb.pub.WtbException;
import com.wtb.qq.domain.QqOauth;

public class QqWriteThread implements Runnable {
	private static final Log log = LogFactory.getLog(QqWriteThread.class);
	private BlockingQueue<QqOauth> qqQueue;			//qq微博发送队列
	private OAuth oauth = new OAuth(Config.QQ_APP_KEY,Config.QQ_APP_SECRET,"");
	private T_API t_api = new T_API();				//微博API
	public QqWriteThread(BlockingQueue<QqOauth> qqQueue){
		this.qqQueue = qqQueue;
	}
	public void run() {
		log.info("QQ写线程已启动");
		while(true){
			QqOauth qqOauth =null ;
			try {
				qqOauth = qqQueue.take();
				log.debug("--------------------------------------------------------"+Thread.currentThread().getName()+"正在运行！");
				oauth.setOauth_token(qqOauth.getOauth_token());
				oauth.setOauth_token_secret(qqOauth.getOauth_token_secrete());
				if (qqOauth.getImageUrl()==null || "".equals(qqOauth.getImageUrl())) {
					t_api.add(oauth, WeiBoConst.ResultType.ResultType_Json, qqOauth.getMessage(), "");
				}else {
					t_api.add_pic(oauth, WeiBoConst.ResultType.ResultType_Json, qqOauth.getMessage(), "", "", "", qqOauth.getImageUrl());
				}
				DataCache.rrlastTime.replace(qqOauth.getRef(), qqOauth.getLastTime());
				DataCache.countIncrement();
			} catch (Exception e) {
				log.error("发送微博失败：", e);
				throw new WtbException("发送微博异常", e);
			}
		}
	}
	
}
